<?php
$width = 120;
$height = 40;
$hb = imagecreate($width, $height);//画布
imagecolorallocate($hb,255,255,255);//画布背景颜色
//随机颜色函数
function randrgb($s = '0123456789ABCDEF', $l = 6)
{
    $str = $s;
    $estr = '';
    $len = strlen($str);
    for ($i = 1; $i <= $l; $i++) {
        $num = rand(0, $len - 1);
        $estr = $estr . $str[$num];
    }
    return $estr;
}
//随机验证码函数
function randyzm($s = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', $l = 4)
{
    $str = $s;
    $estr = '';
    $len = strlen($str);
    for ($i = 0; $i < $l; $i++) {
        $num = rand(0, $len - 1);
        $estr = $estr . $str[$num];
    }
    return $estr;
}
//验证码
$yzm=randyzm();
for($i=0;$i<strlen($yzm);$i++){
    $r=rand(-30,30);
    $color = randrgb();
    $tempBi = imagecolorallocate($hb, '0x' . substr($color, 0, 2), '0x' . substr($color, 2, 2), '0x' . substr($color, 4, 2));
    imagefttext($hb, 25,$r, 5+20*$i , 30, $tempBi , "fonts/REFSAN.TTF", $yzm[$i]);
}
//保存
session_start();
$_SESSION["yzm"]=strtolower($yzm);

//画点画线
for ($i = 0; $i < 50; $i++) {
    $tempX = rand(1, $width - 1);
    $tempY = rand(1, $height - 1);
    $color = randrgb();
    $tempBi = imagecolorallocate($hb, '0x' . substr($color, 0, 2), '0x' . substr($color, 2, 2), '0x' . substr($color, 4, 2));
    if ($i % 5 == 0) {
        $t2X = rand(1, $width - 1);
        $t2Y = rand(1, $height - 1);
        imageline($hb, $tempX, $tempY, $t2X, $t2Y, $tempBi);
    }
    imagestring($hb, 1, $tempX, $tempY, '.', $tempBi);
}
header('Content-Type:image/png;');
imagepng($hb);
?>